package com.loren.tetris.windows.ui;

import com.loren.tetris.data.GameData;
import com.loren.tetris.data.constant.DataConstant;
import com.loren.tetris.data.entity.Block;
import com.loren.tetris.data.entity.BlockShape;
import com.loren.tetris.utils.AssistiveUtils;

import java.awt.*;

public class BoxBlock extends Box {

    public BoxBlock(int boxX, int boxY, int boxW, int boxH, GameData gameData) {
        super(boxX, boxY, boxW, boxH, gameData);
    }

    @Override
    protected void gameAction(Graphics g) {
        Block[][] gameMap = this.getGameData().getGameMap();
        for (int i = 0; i < gameMap.length; i++) {
            for (int j = 0; j < gameMap[i].length; j++) {
                Block block = gameMap[i][j];
                if (block != null) {
                    // 不跟边框重合
                    int x = this.getBoxX() + block.getSize() * i + 1;
                    int y = this.getBoxY() + block.getSize() * j + 1;
                    int size = block.getSize() - 2;
                    g.setColor(block.getColor());
                    if (block.isFrozen()) { // 冻结的
                        g.fillRect(x, y, size, size);
                        g.setColor(DataConstant.COLOR_BLOCK_FROZEN_BORDER);
                        g.drawRect(x, y, size, size);
                    } else { // 非冻结的
                        g.drawRoundRect(x, y, size, size, DataConstant.BLOCK_ROUNDNESS, DataConstant.BLOCK_ROUNDNESS);
                    }
                }
            }
        }

        // 绘制游戏阴影
        if (this.getGameData().isEnableShadow()) {
            BlockShape blockShape = this.getGameData().getCurrent();
            Integer minX = blockShape.getBlockList().stream().map(Block::getX).distinct().min(Integer::compareTo).orElse(0);
            Integer maxX = blockShape.getBlockList().stream().map(Block::getX).distinct().max(Integer::compareTo).orElse(0);
            g.setColor(DataConstant.GAME_SHADOW_COLOR);
            g.fillRect(this.getBoxX() + minX * DataConstant.BLOCK_SIZE, this.getBoxY() + 1, (maxX - minX + 1) * DataConstant.BLOCK_SIZE, this.getBoxH());
        }

        // 绘制暂停提示
        if (this.getGameData().isGamePause()) {
            g.setFont(new Font(AssistiveUtils.getFontFamily(), Font.BOLD, 60));
            g.setColor(DataConstant.COLOR_BOX_TITLE);
            g.drawString("暂停", this.getBoxX() + 90, this.getBoxY() + 270);
        }
    }
}
